package com.bdqn.demo01.filter;


import com.bdqn.demo01.utils.RedisUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class AuthenticationInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从请求头中获取token
        String token = request.getHeader("Authorization");
        RedisUtil redisUtil = new RedisUtil();
        String name = redisUtil.getKey("token");
        //判断是否是登录接口，如果是登录接口，放行，否则判断token是否为空，如果为空，返回false，否则返回true
        if (request.getRequestURI().contains("/acc/getAccount") ){
            return true;
        }
        if (token.isEmpty() || token == null) {
            return false;
        }else if (token.equals(name)) {
            return true;
        }
        return false;
    }
}
